package com.example.viewdemo

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class TestTextview2 : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_test_textview2)

        val testBtn: Button = findViewById(R.id.retrunBtn2)//note 这块要在setContentView 的后面

        val loginBtn: Button = findViewById(R.id.loginBtn)//
        val editText: EditText = findViewById(R.id.editTexttUser)
        val editPassword: EditText = findViewById(R.id.editTextTextPassword)



        testBtn.setOnClickListener {
            finish()
        }

        // 添加 TextWatcher 来监听用户名的变化
        editText.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
                // 文本变化之前调用
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                // 文本变化时调用
                Log.d("login", "user Text changed to: $s")
            }

            override fun afterTextChanged(s: Editable?) {
                // 文本变化之后调用
            }
        })

        editPassword.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                Log.d("login", "password Text changed to: $s")
            }

            override fun afterTextChanged(s: Editable?) {

            }

        }


        )

        loginBtn.setOnClickListener {
            val username = editText.text.toString().trim()
            val password = editPassword.text.toString().trim()


            if (username.isEmpty()) {
                Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show()
                return@setOnClickListener;
            }

            if (password.isEmpty()) {
                Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show()
                return@setOnClickListener;
            }

            if (password.length<8){
                Toast.makeText(this, "密码不能<8", Toast.LENGTH_SHORT).show()
                return@setOnClickListener;
            }

            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show()
        }

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.editmain)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }
}